/**
 * 2011-08-11 角色操作并联
 * huhui
 */

import com.Module.check.PersonalTask;

import common.checkbox.MycheckBox;

import flash.events.MouseEvent;

import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.managers.PopUpManager;
import mx.rpc.events.ResultEvent;

import spark.components.HGroup;
import spark.components.Label;
import spark.components.VGroup;

private function initApp():void
{

//	orgsTreeData= index.orgsTreeAc;
//	orgsTree.dataProvider = orgsTreeData;

//	orgsAction.findById(index.orgid);//查询组织所有员工
	taskObject.taskDistributing();
}


/**
 * 获取选中项
 */
private function getRole(){
//	var orgid : String = orgsTree.treeSelectedItem.id;
//	if(orgPid=="0"){
//		转到溯源组件查询所有流程 
//		tsEntFlow.listFlowEvent(orgid);
//	}
//	else{
		//根据组织id，转到权限组件查询企业设备权限
//		prmsRoleEntEvent.listFlowEvent(orgid);
//	}
	//根据企业获取角色
//	roleRemoteobject.getorgrole(orgid);
//	roleobj.orgPid=Pid;
//	roleRemoteobject.list(roleobj,0,10000);
}


private function listHandler(event:ResultEvent):void{
	listData.refresh();
	var page:Object =  event.result as Object;
	
	listData = page.rows as ArrayCollection;
}
//获取所有用户
private function userlistHandler(event:ResultEvent):void{
	listData.refresh();
	var page:Object =  event.result as Object;
	
	listData = page.rows as ArrayCollection;
}
//获取所有组织id
private function orgidsHandler(event:ResultEvent):void{
	

}


private function listmenuoptHandler(event:ResultEvent):void{

	listmenuopt = event.result as ArrayCollection;
	cretecheckbox();
	
}

/**huhui 2011-08-11
 * 动态实例化模块，菜单，操作checkbox
 */

public function cretecheckbox()
{
	mo_view.removeAllElements();
	var pmsize:int=listmenuopt.length;
	for(var i:int=0;i<pmsize;i++)
	{
		var obj:Object = listmenuopt.getItemAt(i);
		var pmlable:spark.components.Label =new spark.components.Label();
//		Alert.show(obj.toString()+""+obj.organizationName);
		pmlable.text=obj.organizationName;
		pmlable.setStyle("fontSize","16");
		pmlable.setStyle("fontWeight","bold");
		pmlable.setStyle("color","red");
		
		var pmHgroup:VGroup = new VGroup();
		pmHgroup.percentWidth=100;
		pmHgroup.percentHeight=5;
		pmHgroup.paddingLeft=30;
		pmHgroup.addElement(pmlable);
		
		var pmcarrc:ArrayCollection = new ArrayCollection;
//		if(orgPid=="0"){
		 pmcarrc = obj.userlist;
//		}else{
//		pmcarrc = obj.prmsRoleEntEvent;
//		}
		if(pmcarrc!=null&&pmcarrc.length>0)
		{

			var pmcsize:int=pmcarrc.length;
			for(var j:int=0;j<pmcsize;j++)
			{
				var objc:Object=pmcarrc.getItemAt(j);
				objc.childListId = obj.id;
//				objc.orgId = obj.orgid
				var menuopt:HGroup = new HGroup();
				var cmlabe:spark.components.Label = new spark.components.Label();
				cmlabe.text=objc.cuserName;
				var cmlabelCanve:spark.components.BorderContainer = new spark.components.BorderContainer();
				cmlabelCanve.height=20;
				cmlabelCanve.width=200;
				cmlabelCanve.setStyle("borderVisible",false);
				cmlabelCanve.addElement(cmlabe);
				menuopt.addElement(cmlabelCanve);
				var optcheckbox:MycheckBox = new MycheckBox();
				optcheckbox.id = objc.cuserId;
				optcheckbox.value=objc;
				optcheckbox.addEventListener(MouseEvent.CLICK,getoptsel);
				menuopt.addElement(optcheckbox);

					optmap.put(objc.cuserId,optcheckbox);/** 把操作对象放入hashmap */
					optid.push(objc.cuserId);/** 把操作对象id放入Array */


				pmHgroup.addElement(menuopt);
			}
		}
		mo_view.addElement(pmHgroup);
	}
	//根据任务id查询已经分配的人员
	personalTaskObject.findByTaskId(formBean.taskId);
}


/**先把选中状态清空，然后通过数据库数据动态选定 */
public function getroleoptHander(event:ResultEvent)
{
	
	roleopt = event.result as ArrayCollection;
	
	for each(var optitemid:String in optid)
	{
		optmap.getItem(optitemid).selected=false;
	}
	
	if(roleopt!=null)
	{
		
		for each(var obj:Object in roleopt)
		{
			//			Alert.show(optmap.size().toString());
			//			optmap.getItem(obj.coperationId)
			if(optmap.getItem(obj.userId)!=null)
			{
				optmap.getItem(obj.userId).selected=true;
			}
		}
	}
}


public function getoptobj():void
{
	
	//testopt=optmap.getItem("4028850931ac970a0131ac97622c0002") as MycheckBox;
	//testopt.selected=true;
}
public function getoptsel(evt:MouseEvent):void
{
	var opt:MycheckBox = evt.currentTarget as MycheckBox;
	var obj:Object =opt.value;
	optmap.getItem(obj.coperationId).selected = opt.selected;
}


public function save():void
{

	var selopt:ArrayCollection = new ArrayCollection();

	for each (var item:String in optid) {
		var opttem:MycheckBox=optmap.getItem(item) as MycheckBox;
		if(opttem.selected)
		{
			var entEvent:PersonalTask=new PersonalTask;
			entEvent.orgId = (opttem.value).childListId;
			
			entEvent.userId=(opttem.value).cuserId;//操作id
			entEvent.userName = (opttem.value).cuserName;
			
			entEvent.taskId=formBean.taskId;
			entEvent.productId = formBean.productId;
			entEvent.taskAreaId=formBean.taskAreaId;
			entEvent.type = formBean.type;
			entEvent.scanTimes = formBean.scanTimes;
			entEvent.executTime = formBean.executTime;
			selopt.addItem(entEvent);
		}
	}
	if(selopt.length==0)
	{
		Alert.show("请选择稽查人员");
		return ;
	}
	personalTaskObject.save(selopt,formBean.taskId);
	
}

public function saveHandler(event:ResultEvent)
{
	for each(var optitemid:String in optid)
	{
		optmap.getItem(optitemid).selected=false;
	}
	var state:Boolean=event.result.state;
	var msg:String = event.result.msg;
	if(state){
		taskObject.taskAllot(formBean.taskId);
		PopUpManager.removePopUp(this);
		dispatchEvent(new Event("afterSave"));
	}
	Alert.show(msg,"提示");
}



